package cn.iocoder.yudao.module.eval.controller.admin.score;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.module.eval.controller.admin.score.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.score.EvalScoreRecordDO;
import cn.iocoder.yudao.module.eval.service.score.EvalScoreRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 评分记录")
@RestController
@RequestMapping("/eval/score-record")
@Validated
public class EvalScoreRecordController {

    @Resource
    private EvalScoreRecordService scoreRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建评分记录")
    @PreAuthorize("@ss.hasPermission('eval:score-record:create')")
    public CommonResult<Long> createScoreRecord(@Valid @RequestBody EvalScoreRecordSaveReqVO createReqVO) {
        return success(scoreRecordService.createScoreRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新评分记录")
    @PreAuthorize("@ss.hasPermission('eval:score-record:update')")
    public CommonResult<Boolean> updateScoreRecord(@Valid @RequestBody EvalScoreRecordSaveReqVO updateReqVO) {
        scoreRecordService.updateScoreRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评分记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:score-record:delete')")
    public CommonResult<Boolean> deleteScoreRecord(@RequestParam("id") Long id) {
        scoreRecordService.deleteScoreRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得评分记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:score-record:query')")
    public CommonResult<EvalScoreRecordRespVO> getScoreRecord(@RequestParam("id") Long id) {
        EvalScoreRecordDO scoreRecord = scoreRecordService.getScoreRecord(id);
        return success(BeanUtils.toBean(scoreRecord, EvalScoreRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得评分记录分页")
    @PreAuthorize("@ss.hasPermission('eval:score-record:query')")
    public CommonResult<PageResult<EvalScoreRecordRespVO>> getScoreRecordPage(@Valid EvalScoreRecordPageReqVO pageReqVO) {
        PageResult<EvalScoreRecordDO> pageResult = scoreRecordService.getScoreRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EvalScoreRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出评分记录 Excel")
    @PreAuthorize("@ss.hasPermission('eval:score-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportScoreRecordExcel(@Valid EvalScoreRecordPageReqVO exportReqVO,
                                       HttpServletResponse response) throws IOException {
        exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EvalScoreRecordDO> list = scoreRecordService.getScoreRecordList(exportReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "评分记录.xls", "数据", EvalScoreRecordRespVO.class,
                BeanUtils.toBean(list, EvalScoreRecordRespVO.class));
    }

    @GetMapping("/list-by-plan-and-subject")
    @Operation(summary = "根据计划ID和科目ID获得评分记录列表")
    @Parameter(name = "planId", description = "计划ID", required = true, example = "1")
    @Parameter(name = "subjectId", description = "科目ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('eval:score-record:query')")
    public CommonResult<List<EvalScoreRecordRespVO>> getScoreRecordListByPlanAndSubject(
            @RequestParam("planId") Long planId, @RequestParam("subjectId") Long subjectId) {
        List<EvalScoreRecordDO> list = scoreRecordService.getScoreRecordListByPlanAndSubject(planId, subjectId);
        return success(BeanUtils.toBean(list, EvalScoreRecordRespVO.class));
    }

    @PostMapping("/batch-create")
    @Operation(summary = "批量创建评分记录")
    @PreAuthorize("@ss.hasPermission('eval:score-record:create')")
    public CommonResult<Boolean> batchCreateScoreRecords(@Valid @RequestBody EvalScoreBatchCreateReqVO batchReqVO) {
        scoreRecordService.batchCreateScoreRecords(batchReqVO.getScoreRecords());
        return success(true);
    }

}
